package org.hackystat.devcathlon.ui;

import java.util.logging.Logger;

import org.apache.wicket.Request;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebSession;
import org.hackystat.devcathlon.ui.page.matches.MatchesSession;
import org.hackystat.devcathlon.ui.page.profile.ProfileSession;
import org.hackystat.devcathlon.ui.page.profile.browse.ProfileBrowseSession;
//import org.hackystat.devcathlon.ui.page.profile.commentary.ProfileCommentarySession;
import org.hackystat.devcathlon.ui.page.profile.manage.ProfileManageSession;
import org.hackystat.devcathlon.ui.page.scoreboard.ScoreboardSession;
import org.hackystat.devcathlon.ui.page.teams.TeamsSession;
import org.hackystat.devcathlon.ui.page.teams.browse.TeamsBrowseSession;
import org.hackystat.sensorbase.client.SensorBaseClient;


/**
 * Provides a session instance that holds authentication credentials.
 * @author Philip Johnson
 *
 */
public class DevcathlonSession extends WebSession {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  /** The email used to connect to the SensorBase. */
  private String email = null;
  /** The password for the SensorBase. */
  private String password = null;
  /** The current signinFeedback message to display. */
  private String loginFeedback = "";
  /** If this user has been authenticated against the Sensorbase during this session. */
  private boolean isAuthenticated = false;
  /** Session state for the Matches area. */
  private MatchesSession matchesSession = new MatchesSession();
  /** Session state for the Profile area. */
  private ProfileSession profileSession = new ProfileSession();
  /** Session state for the ProfileManage area. */
  private ProfileManageSession profileManageSession = new ProfileManageSession();
  /** Session state for the ProfileBrowse area. */
  private ProfileBrowseSession profileBrowseSession = new ProfileBrowseSession();
  /** Session state for the ProfileCommentary area. */
  //private ProfileCommentarySession profileCommentarySession = new ProfileCommentarySession();
  /** Session state for the Team area. */
  private TeamsSession teamsSession = new TeamsSession();
  /** Session state for the Team Browsw area. */
  private TeamsBrowseSession teamsBrowseSession = new TeamsBrowseSession();
  /** Session state for the Scoreboard area. */
  private ScoreboardSession scoreboardSession = new ScoreboardSession();
  
  /**
   * Provide a constructor that initializes WebSession.
   * @param request The request object.
   */
  public DevcathlonSession(Request request) {
    super(request);
  }

  /**
   * Obtain the current session. 
   * @return The current ProjectBrowserSession.
   */
  public static DevcathlonSession get() {
    return (DevcathlonSession) Session.get();
  }
  
  /**
   * Returns the MatchesSession instance for this session. 
   * @return The MatchesSession instance. 
   */
  public MatchesSession getMatchesSession() {
    return this.matchesSession;
  }
  
  /**
   * Returns the ProfileSession instance for this session. 
   * @return The ProfileSession instance. 
   */
  public ProfileSession getProfileSession() {
    return this.profileSession;
  }
  
  /**
   * Returns the ProfileManageSession instance for this session.
   * @return The ProfileManageSession instance.
   */
  public ProfileManageSession getProfileManageSession() {
    return this.profileManageSession;
  }
  
  /**
   * Returns the ProfileBrowseSession instance for this session.
   * @return The ProfileBrowseSession instance.
   */
  public ProfileBrowseSession getProfileBrowseSession() {
    return this.profileBrowseSession;
  }
  
  /**
   * Returns the ProfileCommentarySession instance for this session.
   * @return The ProfileCommentarySession instance.
   */
  /*public ProfileCommentarySession getProfileCommentarySession() {
    return this.profileCommentarySession;
  }*/
  
  /**
   * Returns the TeamsSession instance for this session. 
   * @return The TeamsSession instance. 
   */
  public TeamsSession getTeamsSession() {
    return this.teamsSession;
  }
 
  /**
   * Returns the TeamsBrowseSession instance for this session.
   * @return The TeamsBrowseSession instance.
   */
  public TeamsBrowseSession getTeamsBrowseSession() {
    return this.teamsBrowseSession;
  }
  
  /**
   * Returns the ScoreboardSession instance for this session. 
   * @return The ScoreboardSession instance. 
   */
  public ScoreboardSession getScoreboardSession() {
    return this.scoreboardSession;
  }
  
  /**
   * Returns true if the user has been authenticated in this session.
   * @return True if the user has supplied a valid email and password for this sensorbase.
   */
  public boolean isAuthenticated() {
    return this.isAuthenticated;
  }
  
  /**
   * Used by the Signin form to provide the SensorBase authentication credentials to this session. 
   * @param user The user.
   * @param password The password. 
   */
  public void setCredentials(String user, String password) {
    this.email = user;
    this.password = password;
  }
  
  /**
   * Returns the string to be displayed in the SigninFeedback label.
   * @return A signin feedback string. 
   */
  public String getLoginFeedback() {
    return this.loginFeedback;
  }
  
  /**
   * Allows other components to set the feedback string for the signin form.
   * @param loginFeedback The message to be displayed.
   */
  public void setLoginFeedback(String loginFeedback) {
    this.loginFeedback = loginFeedback;
  }
 
  
  /**
   * Returns true if this email/password combination is valid for this sensorbase. 
   * @param email The email. 
   * @param password The password.
   * @return True if valid for this sensorbase. 
   */
  public boolean login(String email, String password) {
    try {
      String host = ((DevcathlonApplication)getApplication()).getSensorBaseHost();
      SensorBaseClient client = new SensorBaseClient(host, email, password);
      // Set timeout to 60 seconds just in case. 
      client.setTimeout(1000 * 60);
      client.authenticate();
      this.email = email;
      this.password = password;
      this.isAuthenticated = true;
      return true;
    }
    catch (Exception e) {
      this.isAuthenticated = false;
      return false;
    }
  }

  
  /**
   * Gets the user's email associated with this session. 
   * @return The user.
   */
  public String getUserEmail() {
    return this.email;
  }
 
  
  
  /**
   * @return the password
   */
  public String getPassword() {
    return password;
  }

  /**
   * @return the email
   */
  public String getEmail() {
    return email;
  }
  
  /**
   * @return the logger that associated to this web application.
   */
  public Logger getLogger() {
    return ((DevcathlonApplication)DevcathlonApplication.get()).getLogger();
  }

}
